﻿


var selectedRoles = new Array();
var policeId = 0;
$(function () {

    setAuthority();

    var args = getUrlArgs();

    policeId = parseInt(args["policeId"]);

    $('#RoleListGrid').datagrid({
        url: '/System/LoadRoleList/?policeId=' + policeId,
        title: "角色列表",
        fitColumns: true,
        rownumbers: true,
        pagination: true,
        pageNumber: 1,
        pageSize: 20,
        nowrap: false,
        idField: 'Id',
        toolbar: '',
        columns: [[
               { field: 'ck', checkbox: true },
               { title: 'Id', field: 'Id', align: 'center', width: 10, hidden: true },
               { title: '角色名称', field: 'Name', align: 'left', width: 100 },
               { title: '说明', field: 'Description', align: 'left', width: 100 }
        ]],
        onCheck: function (rowIndex, rowData) {
            var key = rowData.Id;
            addRoles(key);
        },
        onUncheck: function (rowIndex, rowData) {
            var key = rowData.Id;
            delRoles(key);
        },
        onSelectAll: function (rows) {
            for (var i = 0; i < rows.length; i++) {
                addRoles(rows[i].Id);
            }
        },
        onUnselectAll: function (rows) {
            for (var i = 0; i < rows.length; i++) {
                delRoles(rows[i].Id);
            }
        },
        onLoadSuccess: function (data) {
            for (var i = 0; i < data.rows.length; i++) {
                selectedRoles = data.rows[0].CheckedId.split(",");
                var key = data.rows[i].Id;
                var exist = false;
                for (var j = 0; j < selectedRoles.length; j++) {
                    if (key == selectedRoles[j]) {
                        exist = true;
                        $('#RoleListGrid').datagrid("checkRow", i);
                        break;
                    }
                }
                if (!exist)
                    $('#RoleListGrid').datagrid("uncheckRow", i);
            }
        }
    });
});

function setAuthority() {
    $.ajax({
        url: "/System/CheckAuthority",
        type: "POST",
        async: false,
        dataType: "json",
        data: { "roleKey": 'admin' },
        success: function (req) {
            if (req.IsSuccess) {
                $('#btnSavePoliceRole').show(); 
            } else {
                $('#btnSavePoliceRole').hide(); 
            }

            if (req.Data) {
                $.messager.alert('提示', req.Data);
            }
        },
    });
}

function addRoles(key) {
    for (var i = 0; i < selectedRoles.length; i++) {
        if (key == selectedRoles[i])
            return;
    }
    selectedRoles.push(key);
};
function delRoles(key) {
    for (var i = 0; i < selectedRoles.length; i++) {
        if (key == selectedRoles[i]) {
            selectedRoles = selectedRoles.slice(0, i).concat(selectedRoles.slice(i + 1, selectedRoles.length));
            return;
        }
    }
};

function btnSavePoliceRoleAction() {
    var roles = selectedRoles.join(',');
    //var rolestr = roles.substring(1, roles.length);
    $.ajax({
        url: "/System/SavePoliceRole",
        type: "POST",
        dataType: "json",
        data: { "Roles": roles, "policeId": policeId },
        async: false,
        success: function (req) {
            if (req.IsSuccess) {
                $.messager.alert('提示', '保存成功!', "info");
            } else {
                $.messager.alert('提示', '保存失败!' + req.Message, "error");
            }
        },
    });
};

function btnCancelPoliceRoleAction() {
    parent.art.dialog.list['diaForPoliceRoleView'].close();
};